<template>
	<view class="h-fill">
		<u-upload :custom-btn="true" ref="uUpload" :header="header" :action="action"
			:show-upload-list="false" :autoUpload="true" @on-success="onUploadSuccess"> 
			<view slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
				<!-- <image v-if="imageUrl" :src="imageUrl" mode="aspectFit" 
					:style="[imgStyle]"></image>
				<view v-else class="btn-add">
					<u-icon name="plus" size="60" :color="$u.color['lightColor']"></u-icon>
				</view> -->
				<slot></slot>
			</view>
		</u-upload>
	</view>
</template>

<script>
	import { API } from '@/api.js'
	import { BASE_URL } from '@/const.js'
	const token = uni.getStorageSync('token')
	export default {
		data() {
			return {
				action: `${BASE_URL}${API.upload}`,
				header: {
					"Authorization": 'bearer ' + token
				},
				imageUrl: ""
			};
		},
		props: {
			value: {
				type: String,
				default: ""
			},
			imgStyle: {
				type: Object,
				default: () => {
					return {};
				}
			}
		},
		watch: {
			value(newVal) {
				this.imageUrl = newVal;
			}
		},
		created() {
			this.imageUrl = this.value;
		},
		methods: {
			initFileList(value) {
				this.fileList = [];
				if (value) {
					this.fileList.push({
						url: value
					});
				}
				this.$log("初始化文件列表", this.fileList);
			},
			onUploadSuccess(data, index, lists) {
				this.$log("上传成功", data);
				this.imageUrl = data.url;
				this.$refs.uUpload.clear();
				this.$emit("input", this.imageUrl);
			}
		}
	}
</script>

<style scoped>
	.slot-btn {
		/* width: 100vw; */
	}
	.btn-add {
		background-color: #EEEEEE;
		padding: 10rpx;
		border-radius: 10rpx;
	}
</style>
